<template>
  <ele-page>
    <!-- 搜索表单 -->
    <search @search="reload" />
    <ele-card :body-style="{ paddingTop: '8px' }">
      <!-- 表格 -->
      <ele-pro-table
        ref="tableRef"
        :columns="columns"
        :datasource="datasource"
        :show-overflow-tooltip="true"
        highlight-current-row
        border
        stripe
        :where="defaultWhere"
      >
        <template #status="{ row }">
          <el-tag :type="statusType(row.repay_status)">
            {{ statusText(row.repay_status) }}
          </el-tag>
        </template>
        <template #repay_amount="{ row }">
          <span class="pointer blue" @click="openRepayRecord(row)">{{
            row.repay_amount
          }}</span>
        </template>
        <template #deduction_amount="{ row }">
          <span v-if="row.deduction_amount == '无'">无</span>
          <span v-else class="pointer blue" @click="openPrepay(row)">{{
            row.deduction_amount
          }}</span>
        </template>
        <!-- <template #toolbar="">
          <el-button type="primary" @click="openRepay()"> 批量返还 </el-button>
        </template> -->
        <template #action="{ row }">
          <el-button-group v-if="chooseStatus == 0">
            <el-button type="success" size="small" @click="openRepay(row)">
              已返还
            </el-button>
          </el-button-group>
        </template>
      </ele-pro-table>
      <!-- 账单详情 -->
      <detail v-model="showDetail" :data="current" />
      <!-- 还款操作 -->
      <repay v-model="showRepay" :data="current" @done="reload" />
      <!-- 还款记录 -->
      <repay-record v-model="showRepayRecord" :data="current" />
    </ele-card>
  </ele-page>
</template>

<script setup>
  import { ref } from 'vue';
  import Search from './components/search.vue';
  import Detail from './components/detail.vue';
  import Repay from './components/repay.vue';
  import RepayRecord from './components/repay-record.vue';

  import { EleMessage } from 'ele-admin-plus/es';
  import { pageReturnMoney } from '@/api/company';

  const statusText = (value) => {
    const cs = {
      1: '已还清',
      0: '待还款'
    };
    return cs[value];
  };
  const statusType = (state) => {
    const stateMap = {
      1: 'success',
      0: 'warning'
    };
    return stateMap[state];
  };

  /** 表格实例 */
  const tableRef = ref(null);

  /** 默认条件 */
  const defaultWhere = {
    status: 0
  };

  /** 表格列配置 */
  const columns = ref([
    {
      prop: 'id',
      label: 'ID',
      width: 85,
      align: 'center'
    },
    {
      width: 120,
      prop: 'plate_number',
      label: '车牌号码'
    },
    {
      prop: 'company',
      label: '车队'
    },
    {
      width: 100,
      prop: 'realname',
      label: '车主'
    },
    {
      width: 120,
      prop: 'tel',
      label: '联系电话'
    },
    {
      width: 100,
      prop: 'amount',
      label: '返还金额'
    },

    {
      columnKey: 'action',
      label: '操作',
      slot: 'action',
      width: 130,
      align: 'center',
      fixed: 'right'
    }
  ]);

  /** 表格选中数据 */
  const selections = ref([]);

  /** 当前编辑数据 */
  const current = ref(null);

  /** 表格数据源 */
  const datasource = ({ page, limit, where, orders }) => {
    return pageReturnMoney({
      ...where,
      ...orders,
      page,
      limit
    });
  };

  /** 账单详情 */
  const showDetail = ref(false);
  const openDetail = (row) => {
    current.value = row;
    showDetail.value = true;
  };

  /** 还款操作 */
  const showRepay = ref(false);
  const openRepay = (row) => {
    current.value = row;
    showRepay.value = true;
  };

  /** 还款记录 */
  const showRepayRecord = ref(false);
  const openRepayRecord = (row) => {
    current.value = row;
    showRepayRecord.value = true;
  };

  /** 提前还款详情 */
  const showPrepay = ref(false);
  const openPrepay = (row) => {
    current.value = row;
    showPrepay.value = true;
  };

  /** 搜索 */
  const chooseStatus = ref(0);
  const reload = (where) => {
    chooseStatus.value = where ? where.choose_status : 1;
    selections.value = [];
    tableRef.value?.reload?.({ page: 1, where });
  };
</script>
